package corp.edu.amadeus.core.business.service.impl;

import java.util.Collection;

import org.apache.log4j.Logger;

import corp.edu.amadeus.core.business.locator.ObjectLocator;
import corp.edu.amadeus.core.business.object.impl.TextBO;
import corp.edu.amadeus.core.business.service.ifc.ITextService;
import corp.edu.amadeus.core.facade.ifc.IGenericCRUDFacade;
import corp.edu.amadeus.core.facade.ifc.IGenericSearchFacade;
import corp.edu.amadeus.utils.date.DateUtils;

public class TextServiceImpl implements ITextService {

	private IGenericCRUDFacade cFac;
	private IGenericSearchFacade sFac;
	private static Logger log = Logger.getLogger(TextServiceImpl.class);

	public TextServiceImpl(IGenericCRUDFacade cFac,
			IGenericSearchFacade sFac) {
		this.cFac = cFac;
		this.sFac = sFac;
	}

	public void addText(TextBO text) {
		text.setDateCreated(DateUtils.getNow());
		text.setDateModified(DateUtils.getNow());
		this.cFac.persist(text);
		this.cFac.flush();
	}

	public void addText(String text, String lang) {
		TextBO textBO = ObjectLocator.getText();
		textBO.setDateCreated(DateUtils.getNow());
		textBO.setDateModified(DateUtils.getNow());
		textBO.setText(text);
		textBO.setLang(lang);
		this.cFac.persist(textBO);
		this.cFac.flush();
	}

	public void updateText(TextBO text) {
		text.setDateModified(DateUtils.getNow());
		this.cFac.update(text);
		this.cFac.flush();
	}

	public void updateText(int textId, String text,
			String lang) throws Exception {
		TextBO textBO = (TextBO)this.sFac.findByKey(TextBO.class,"persistenceId", textId);
		textBO.setText(text);
		textBO.setDateModified(DateUtils.getNow());
		textBO.setLang(lang);
		this.cFac.update(textBO);
		this.cFac.flush();
	}

	public void deleteText(int textId) throws Exception {
		TextBO textBO = (TextBO)this.sFac.findByKey(TextBO.class,"persistenceId", textId);
		this.cFac.delete(textBO);
		this.cFac.flush();
	}

	public Collection<TextBO> getAllTexts() {
		String query = "from TextBO order by id asc";
		return this.sFac.findByQuery(query);
	}

	public TextBO getText(int textId)
		throws Exception {
		return (TextBO)this.sFac.findByKey(TextBO.class,"persistenceId", textId);
	}


}